import Promise from '../libs/promise.js'

var storage = {};
var draftStorageManager;

var keys = {
    draftStorageKey: "draft"
}

function getStorage(key) {
    return new Promise((s, f) => {
        wx.getStorage({
            key: key,
            success: function (res) {
                s(res);
            },
            fail: function (res) {
                f(res)
            },
            complete: function () {
                // complete
            }
        })
    })
}

function setStorge(key, data) {
    return new Promise((s, f) => {
        wx.setStorage({ key: key, data: data, s, f })
    })
}


draftStorageManager = {
    getAllDrafts: function () {
        return new Promise((s, f) => {
            getStorage(keys.draftStorageKey).then(res => {
                s(res);
            }).catch(e => {
                f(e);
            })
        })
    },
    addDraft: function (draft) {
        var drafts;
        return draftStorageManager.getAllDrafts().then(res => {
            drafts = res.data;

            if (!!drafts) {
                drafts = drafts.concat(draft);
                setStorge(keys.draftStorageKey, drafts);
            } else {
                drafts = [draft];
                setStorge(keys.draftStorageKey, drafts);
            }
        }).catch(e => {
            drafts = [draft];
            setStorge(keys.draftStorageKey, drafts);
        })
    },
    searchDraft: function (draftId) {
        var drafts;
        return new Promise((s, f) => {
            draftStorageManager.getAllDrafts().then(res => {
                drafts = res.data;

                for (var i in drafts) {
                    if (drafts[i].id == draftId) {
                        s(drafts[i]);
                        break;
                    }
                    if (i == drafts.length - 1) {
                        f("nodata");
                    }
                }
            }).catch(e => {
                f(e);
            })
        })
    },
    deleteDraft: function (draftId) {
        var drafts;
        return new Promise((s, f) => {
            draftStorageManager.getAllDrafts().then(res => {
                drafts = res.data;

                for (var i in drafts) {
                    if (drafts[i].id == draftId) {
                        drafts.splice(i, 1);
                        setStorge(keys.draftStorageKey, drafts);
                        s(drafts[i]);
                        break;
                    }
                    if (i == drafts.length - 1) {
                        f("nodata");
                    }
                }
            }).catch(e => {
                f(e);
            })
        })
    },
    updateDraft: function (draft) {
        var drafts;
        return new Promise((s, f) => {
            draftStorageManager.getAllDrafts().then(res => {
                drafts = res.data;

                for (var i in drafts) {
                    if (drafts[i].id == draft.id) {
                        drafts[i] = draft;
                        setStorge(keys.draftStorageKey, drafts);
                        s(drafts[i]);
                        break;
                    }
                    if (i == drafts.length - 1) {
                        f("nodata");
                    }
                }
            }).catch(e => {
                f(e);
            })
        })
    }
}

storage.draftStorageManager = draftStorageManager;

module.exports = storage;